<?php
include_once "controle/db.php";

//$driver = "mysql;localhost;sgm_db;sgm_user;FMs-se5ql0F3;";
//$driver = "mysql;localhost;angulowe_sgm;angulowe_sgm;FMs-se5ql0F3;";
//$driver = "mysql;localhost;sgm;root;mandriva;";
$db = new db($driver);

$sql_dup = "SELECT codebar FROM sgm_produto WHERE id_empresa = 1 GROUP BY codebar HAVING count(codebar) > 1";
$conexao = $db->conecta();
$result = $db->query($sql_dup, $conexao);
while($codebar = $db->fetch_array($result)){
    //echo $codebar[0]."<br>\n";
    
    $sql_id = "SELECT id FROM sgm_produto WHERE id_empresa = 1 AND codebar = '".$codebar[0]."'";
    $res_id = $db->query($sql_id, $conexao);
    
    $idc = 0;
    while($id1 = $db->fetch_array($res_id)){
        $id[$idc] = $id1[0];
        $idc++;
    }
    
    if($id[0] < $id[1]){
        $sql_del = "DELETE FROM sgm_produto WHERE id = '".$id[0]."'";
    }else{
        $sql_del = "DELETE FROM sgm_produto WHERE id = '".$id[1]."'";
    }
    echo "$sql_del".$db->query($sql_del, $conexao)."<br>\n";
    
}
$db->close($conexao);